package mysql

import (
	"novel-hub/novelrpc/novelrpc"

	"gorm.io/gorm"
)

type Novel struct {
	gorm.Model
	Title    string `gorm:"type:varchar(30);not null;comment:'标题'"`
	Category string `gorm:"type:varchar(30);not null;comment:'分类'"`
	Label    string `gorm:"type:varchar(30);comment:'标签'"`
	Intro    string `gorm:"type:varchar(300);not null;comment:'简介'"`
}

func (n *Novel) NovelAdd(db *gorm.DB) error {
	return db.Debug().Create(n).Error
}

func (n *Novel) FindNovelByTitleCategory(db *gorm.DB, title string, category string, in *novelrpc.NovelListRequest, lists []*novelrpc.NovelList) ([]*novelrpc.NovelList, error) {
	/*
		SELECT
		novels.id as novel_id,novels.title,novels.category,novels.label,novels.intro
		FROM
		novels
		WHERE novels.title= '' OR novels.category ='科幻'
		ORDER BY created_at DESC
	*/
	err = db.Debug().Model(n).Select("novels.id as novel_id,novels.title,novels.category,novels.label,novels.intro").
		Where("novels.title like ? and novels.category like ?", "%"+title+"%", "%"+category+"%").
		Order("novels.created_at desc").
		Scopes(Paginate(int(in.Page), int(in.Size))).Find(&lists).Error
	return lists, err
}
